// pages/user/map_address.js
var app = getApp();
var setting = app.globalData.setting;
var request = app.request;
var common = require('../../../utils/common.js');
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		url: setting.url,
		array: ['南宁市', '桂林市', '防城港市', '柳州市'],
		index: 0,
		merchant_name: '',
		Merchlist: [],
		Merchlist1: [],
		data: {
			latitude: '0', //首次加载维度
			longitude: '0', //首次加载的经度
			scale: 16,
			mapName: "", //选点的位置
			key: '地图key', //地图key
			address: '', //任务地址
			authentication: '认证',
			markers: ''
		},
		key: 'BDFBZ-ZMWW3-R6U3M-RRFAB-VQZCS-I5BMJ',
		pickupList: [],
		info: null
	},
	confirm(e) {
		let data = e.currentTarget.dataset.id
		this.setData({
			longitude: data.longitude,
			latitude: data.latitude,
			merchant_name: '',
			Merchlist: [],
			info: data
		});
	},
	handlerInput(e) {
		let val = e.detail.value;
		let Merchlist = this.data.Merchlist1;
		if (val) {
			let a = []
			Merchlist.filter(item => {
				if (item.shop_name.indexOf(val) != -1) {
					a.push(item)
				}
			})
			this.setData({
				Merchlist: a
			})
		} else {
			this.setData({
				Merchlist: []
			})
		}
	},
	bindPickerBuyersName(e) {
		let i = e.detail.value;
		let name = this.data.Merchlist[i].shop_name;
		let id = this.data.Merchlist[i].id;
		this.setData({
			merchant_name: name,
			merch_id: id,
			Merchlist: this.data.Merchlist1
		})
	},
	bindPickerChange: function(e) {
		this.setData({
			index: e.detail.value
		})
	},
	tz() {
		let pages = getCurrentPages();
		let prevPage = pages[pages.length - 2];
		console.log(this.data.info,222)
		prevPage.setData({
			message: this.data.info,
		})
		wx.navigateBack({
			delta: 1,
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		console.log(this.data)
		let that = this
		qqmapsdk = new QQMapWX({
			key: that.data.key
		});
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		let that = this
		wx.getLocation({
			isHighAccuracy: true,
			type: 'gcj02',
			success: res => {
				let msk = [{
					id: 0, //标记点 id
					width: 20,
					height: 20,
					longitude: res.longitude,
					latitude: res.latitude,
					label: {
						content: '我的位置',
						color: '#409eff',
						fontSize: 15
					},
				}]
				that.setData({
					longitude: res.longitude,
					latitude: res.latitude,
					markers: msk
				});
				// that.addMark(res)
			}
		})
	},
	// 搜索框触发
	nearby_search: function(e) {
		var _this = this;
		let data = this.data.longitude + ',' + this.data.latitude
		// 调用接口
		qqmapsdk.getSuggestion({
			keyword: e.detail.value, //搜索关键词
			success: function(res) { //搜索成功后的回调
				var mks = [{
					id: 0, //标记点 id
					width: 20,
					height: 20,
					longitude: _this.data.longitude,
					latitude: _this.data.latitude,
					label: {
						content: '我的位置',
						color: '#409eff',
						fontSize: 15
					},
				}]
				for (var i = 0; i < res.data.length; i++) {
					mks.push({ // 获取返回结果，放到mks数组中
						title: res.data[i].title,
						id: res.data[i].id,
						latitude: res.data[i].location.lat,
						longitude: res.data[i].location.lng,
						width: 20,
						height: 20,
						address: res.data[i].address
					})
				}
				_this.setData({ //设置markers属性，将搜索结果显示在地图中
					shopList: res.data,
					markers: mks
				})
				wx.stopPullDownRefresh();
			},
			fail: function(res) {
				console.log(res);
			},
			complete: function(res) {
				console.log(res);
			}
		});
	},
	getList(res) {
		var that = this;
		let data = {
			longitude: res.longitude,
			latitude: res.latitude
		}
		request.get('/api/user/shop_list', {
			data,
			success: function(r) {
				that.setData({
					shopList: r.data.result
				});
				wx.stopPullDownRefresh();
			}
		});
	},
	geiadd(e) {
		let data = e.currentTarget.dataset.id
		this.setData({
			longitude: data.location.lng,
			latitude: data.location.lat,
			info: data
		});
	},
	// 地图点击事件
	controltap(e){
		// console.log(e)
	},
	addMark(res) {
		var that = this;
		var marker = [{
			id: 0, //标记点 id
			width: 28,
			height: 28,
			longitude: res.longitude,
			latitude: res.latitude,
			label: {
				content: '我的位置',
				color: '#409eff',
				fontSize: 15
			},
		}]
		let data = {
			longitude: res.longitude,
			latitude: res.latitude
		}
		request.get('/api/user/shop_list', {
			data,
			success: function(r) {
				r.data.result.forEach((s, index) => {
					marker.push({
						id: index + 1, //标记点 id
						width: 28,
						height: 28,
						longitude: s.longitude,
						latitude: s.latitude,
						label: {
							content: s.shop_name,
							color: '#f00',
							fontSize: 15
						},
					})

				})
				that.setData({
					markers: marker,
					shopList: r.data.result,
					Merchlist1: r.data.result
				});
				wx.stopPullDownRefresh();
			}
		});
		// wx.getLocation({
		// 	type: 'gcj02',
		// 	success: res => {
		// 		this.setData({
		// 			longitude: res.longitude,
		// 			latitude: res.latitude
		// 		})
		// 		this.addMark(res)
		// 	}
		// })
	},



	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},


	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})